import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import LayoutView from '../views/LayoutView.vue'
import { useUserStore, UserRole } from '@/stores/userStore'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: { requiresAuth: false },
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView,
      meta: { requiresAuth: false },
    },
    {
      path: '/',
      component: LayoutView,
      meta: { requiresAuth: true },
      children: [
        // 管理员路由
        {
          path: 'admin/welcome',
          name: 'adminWelcome',
          component: () => import('../views/admin/AdminWelcomeView.vue'),
          meta: { requiresAuth: true, role: UserRole.ADMIN },
        },
        {
          path: 'admin/landslide',
          name: 'adminLandslide',
          component: () => import('../views/admin/LandslideManagementView.vue'),
          meta: { requiresAuth: true, role: UserRole.ADMIN },
        },
        {
          path: 'admin/users',
          name: 'adminUsers',
          component: () => import('../views/admin/UserManagementView.vue'),
          meta: { requiresAuth: true, role: UserRole.ADMIN },
        },
        // 普通用户路由
        {
          path: 'user/welcome',
          name: 'userWelcome',
          component: () => import('../views/user/UserWelcomeView.vue'),
          meta: { requiresAuth: true, role: UserRole.USER },
        },
        // 地图功能路由（所有用户可访问）
        {
          path: 'map/view',
          name: 'mapView',
          component: () => import('../views/map/MapView.vue'),
          meta: { requiresAuth: true },
        },
        // 统计分析路由（所有用户可访问）
        {
          path: 'statistics',
          name: 'statistics',
          component: () => import('../views/statistics/StatisticsView.vue'),
          meta: { requiresAuth: true },
        },
      ],
    },
  ],
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 如果访问的是根路径，根据登录状态和角色重定向
  if (to.path === '/' && to.name === undefined) {
    if (userStore.isLoggedIn) {
      // 已登录，根据角色重定向到对应的欢迎页面
      if (userStore.userInfo?.role === UserRole.ADMIN) {
        return next('/admin/welcome')
      } else {
        return next('/user/welcome')
      }
    } else {
      // 未登录，重定向到登录页面
      return next('/login')
    }
  }

  // 检查路由是否需要认证
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    // 如果需要认证但用户未登录，重定向到登录页面
    return next('/login')
  }

  // 检查路由是否需要特定角色
  if (to.meta.role && userStore.userInfo?.role !== to.meta.role) {
    // 如果需要特定角色但用户角色不匹配，重定向到对应的欢迎页面
    if (userStore.userInfo?.role === UserRole.ADMIN) {
      return next('/admin/welcome')
    } else {
      return next('/user/welcome')
    }
  }

  // 其他情况正常导航
  next()
})

export default router
